package com.futures.questionbank.main;

import android.text.TextUtils;
import android.widget.EditText;

import com.allen.library.SuperButton;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.table.User;
import com.futures.questionbank.db.table.User_Table;
import com.futures.questionbank.view.CustomLoadingDialog;
import com.raizlabs.android.dbflow.sql.language.Select;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/6/15 0015 16:39
 * version: 1.0
 */
public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password_two)
    EditText etPasswordTwo;
    @BindView(R.id.sb_ok)
    SuperButton sbOk;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.forget_password_activity);
        setTitle("忘记密码");
    }

    @Override
    protected void loadData() {

    }



    @OnClick(R.id.sb_ok)
    public void onViewClicked() {
        if (!UItils.isPhoneNumber(etPhone.getText().toString().trim())) {
            UItils.showToastSafe("请输入正确的手机号码");
            return;
        }

        if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
            UItils.showToastSafe(etPassword.getHint().toString());
            return;
        }
        if (TextUtils.isEmpty(etPasswordTwo.getText().toString().trim())) {
            UItils.showToastSafe(etPasswordTwo.getHint().toString());
            return;
        }

        if (!TextUtils.equals(etPassword.getText().toString().trim(),etPasswordTwo.getText().toString().trim())){
            UItils.showToastSafe("2次输入密码不一致");
            return;
        }

        User user = new Select()
                .from(User.class)
                .where(User_Table.phone.is(etPhone.getText().toString().trim()))
                .querySingle();
        if (null != user){
            if (user.getPhone().equals(etPhone.getText().toString().trim())){
                user.setPassword(etPassword.getText().toString().trim());
                user.update();
                finish();
            }else {
                UItils.showToastSafe("帐号输入有误");
            }
        }else {
            UItils.showToastSafe("帐号未注册");
            return;
        }

        CustomLoadingDialog.dismiss(this);
        UItils.startActivity(MainActivity2.class);
        finish();
    }
}
